/**
 * 资产管理
 */
Ext.ns("Ext.lams");
Ext.lams.AssetsGrid = Ext.extend(Ext.grid.GridPanel, {
			constructor : function(_cfg) {
				Ext.apply(this, _cfg || {});
				Ext.lams.AssetsGrid.superclass.constructor.call(this, {
							frame : true,
							loadMask : {
								msg : '正在载入数据,请稍等...'
							},
							listeners : {
								rowdblclick : function(g) {
									var r = g.getSelectionModel().getSelected();
									this.fireEvent("rowdb", this, r);
									// alert(d.data);
								}
							}
						});
				this.addEvents("rowdb");
				this.addEvents("edit");
				this.addEvents("delete");
			},
			initComponent : function() {
				var _store = new Ext.data.JsonStore({
							proxy : new Ext.data.HttpProxy({
										url : 'queryAssetsAction.action',
										method : 'GET'
									}),
							totalProperty : 'totalCount',
							root : 'data',
							fields : ['ASSET_ID', 'ASSET_NO', 'ASSET_NAME', {
										name : 'BUYDATE'
									}, 'MADE_FAC', {
										name : 'ASSET_PRICE',
										type : 'float'
									}, 'ASSET_TYPE']
						});
				var _cm = new Ext.grid.ColumnModel([{
							header : '资产id',
							hidden : true,
							dataIndex : 'ASSET_ID'
						}, {
							header : '资产编号',
							dataIndex : 'ASSET_NO'
						}, {
							header : '资产名称',
							dataIndex : 'ASSET_NAME'
						}, {
							header : '资产类别',
							dataIndex : 'ASSET_TYPE'
						}, {
							header : '购买日期',
							dataIndex : 'BUYDATE'
						}, {
							header : '生产厂家',
							dataIndex : 'MADE_FAC'
						}, {
							header : '资产价值',
							dataIndex : 'ASSET_PRICE'
						}, {
							header : "",
							width : 30,
							renderer : function(v, d, r, ridx, cidx, s) {
								return me.edit_render(r);
							}
						}, {
							header : "",
							width : 30,
							renderer : function(v, d, r, ridx, cidx, s) {
								return me.delete_render(r);
							}
						}]);
				var _sm = new Ext.grid.RowSelectionModel({
							singleSelect : true
						});
				var _bbar = new Ext.lams.comp.PagingToolbar({
							store : _store
						});
				var _
				Ext.apply(this, {
							store : _store,
							cm : _cm,
							sm : _sm,
							bbar : _bbar
						})
				Ext.lams.AssetsGrid.superclass.initComponent.call(this);
			},
			loadData : function(_r, fn) {
				var ds = this.getStore();
				ds.baseParams = _r.data;
				ds.load({
							params : {
								start : 0,
								limit : this.getBottomToolbar().pageSize
							},
							callback : function() {
								fn();
							}
						});
			},
			getSelectedItem : function() {
				var _sel = this.getSelectionModel();
				if (_sel.getCount() == 0)
					throw new Error("未选中某条记录");
				return _sel.getSelected();
			},
			edit_render : function(recode) {
				var me = this;
				var contentId = Ext.id();
				var btn = createGridButton.defer(1, this, [contentId]);
				function createGridButton() {
					return new Ext.Button({
								iconCls : 'silk-user-edit',
								// width:16,
								tooltip : '编辑',
								handler : function() {
									me.fireEvent('edit', recode, me);
								}
							}).render(document.body, contentId);
				}
				return ('<div id="' + contentId + '"/>');
			},
			delete_render : function(recode) {
				var me = this;
				var contentId = Ext.id();
				var btn = createGridButton.defer(1, this, [contentId]);
				function createGridButton() {
					return new Ext.Button({
								iconCls : 'silk-user-delete',
								tooltip : '删除',
								handler : function() {
									me.fireEvent('delete', recode, me);
								}
							}).render(document.body, contentId);
				}
				return ('<div id="' + contentId + '"/>');
			}
		});
Ext.lams.AssetInfoForm = Ext.extend(Ext.form.FormPanel, {
			constructor : function(_cfg) {
				Ext.apply(this, _cfg);
				Ext.lams.AssetInfoForm.superclass.constructor.call(this, {
							layout : 'form',
							frame : true,
							defaults : {
								xtype : 'textfield',
								anchor : '95%',
								labelWidth : 65
							}

						})
			},
			initComponent : function() {
				this.items = [{
							hidden : true,
							name : 'ASSET_ID'
						}, {
							fieldLabel : '资产号',
							allowBlank : false,
							blankText : '请填写资产号',
							name : 'ASSET_NO'
						}, {
							fieldLabel : '资产名称',
							name : 'ASSET_NAME',
							allowBlank : false,
							blankText : '请填写资产名称'
						}, {
							fieldLabel : '资产类别',
							xtype : 'combo_scode',
							code_cate : 'ASSET_TYPE',
//							params : {
//								'codecate' : 'ASSET_TYPE'
//							},
							hiddenName : 'ASSET_TYPE',
							allowBlank : false,
							blankText : '请选择资产类别'
						}, {
							fieldLabel : '生产厂家',
							name : 'MADE_FAC'
						}, {
							xtype : 'datefield',
							value : new Date(),
							format : "Y-m-d",
							fieldLabel : '购买时间',
							name : 'BUYDATE'
						}, {
							fieldLabel : '资产价值',
							name : 'ASSET_PRICE'
						}, {

						}];
				Ext.lams.AssetInfoForm.superclass.initComponent.call(this);
			},
			setValue : function(_r) {
				this.getForm().loadRecord(_r);
			},
			getValue : function() {
				if (this.getForm().isValid())
					return new Ext.data.Record(this.getForm().getValues());
				else
					throw Error("表单填写出错");
			},
			clear : function() {
				this.getForm().reset();
			},
			submit : function(fn) {
				if (this.getForm().isValid()) {
					this.getForm().submit({
								waitMsg : "正在保存数据...",
								url : "addOrUpdateAssetAction.action",
								method : "POST",
								success : function(form, action) {
									fn(form, action);
								},
								faild : function(form, action) {
									fn(form, action);
								}
							})
				}
			}
		});
Ext.lams.infoWin = Ext.extend(Ext.Window, {
			// form : {},
			constructor : function(_cfg) {
				Ext.apply(this, _cfg || {});
				Ext.lams.infoWin.superclass.constructor.call(this, {
							closeAction : "hide"
						});
				this.addEvents("submit");
			},
			initComponent : function() {
				var form = new Ext.lams.AssetInfoForm();
				this.form = form;
				Ext.apply(this, {
							items : [form],
							buttons : [{
										xtype : 'btn_save',
										handler : this.saveData,
										scope : this
									}, {
										xtype : 'btn_cancel',
										handler : this.cancel,
										scope : this
									}]
						});
				Ext.lams.infoWin.superclass.initComponent.call(this);
			},
			saveData : function(btn) {
				var me = this;
				btn.disable();
				try {
					// _r = this.getForm().getValue();
					this.getForm().submit(function(from, action) {
								// Ext.Msg.alert("INFO",
								// action.result["message"]);
								btn.enable();
								if (action.result["success"]) {
									me.fireEvent("submit", this, action)
								} else {
									// Ext.Msg.alert("INFO",action.result["message"])
									H.Msg.error(action.result["message"]);
								}
							});
				} catch (_e) {
					// Ext.MessageBox.alert("ERROR", _e.message);
					H.Msg.error(_e.message);
				}

			},
			getForm : function() {
				return this.form;
			},
			cancel : function() {
				this.getForm().clear();
				this.hide();
			}
		});
Ext.lams.queryForm = Ext.extend(Ext.form.FormPanel, {
			constructor : function(_cfg) {
				Ext.apply(this, _cfg || {});
				Ext.lams.queryForm.superclass.constructor.call(this, {
							layout : 'form',
							frame : true
						});
			 	this.addEvents("query");
			},
			initComponent : function() {
				var me = this;
				this.items = [{
					anchor : '100%',
					baseCls : 'x-plain',
					layout : 'hbox',
					layoutConfig : {
						padding : 5
					},
					defaults : {
						margins : '0 15 0 0'
					},
					// defaults:{labelWidth:65},
					items : [{
						layout : 'form',
						flex : .3,
						items : [{
							xtype : 'xauto',
							fieldLabel : '资产编号/名称',
							emptyText : '请输入资产号或名称',
							params : {
								sqlCode : 'SEL_ASSET001'
							},
							hiddenName : 'ASSET_ID',
							fields : [{
										name : "ASSET_ID"
									}, {
										name : "ASSET_NO"
									}, {
										name : "ASSET_NAME"
									}],
							valueField : "ASSET_ID",
							displayField : "ASSET_NAME",
							tpl : new Ext.XTemplate(
									'<tpl for="."><div class="x-combo-list-item">',
									'{ASSET_NO}--{ASSET_NAME}', '</div></tpl>')
						}]
					}, {
						layout : 'form',
						flex : .3,
						items : [{
									fieldLabel : '资产类别',
									xtype : 'combo_scode',
									code_cate:'ASSET_TYPE',
//									params : {
//										'codecate' : 'ASSET_TYPE'
//									},
									hiddenName : 'ASSET_TYPE'
								}]
					}, {
						xtype : "btn_query",
						handler : function() {
							me.fireEvent("query",this,me)
						}
					}, {
						xtype : "btn_new",
						handler : function() {

						}
					}, {
						flex : .3
					}]
				}];
				Ext.lams.queryForm.superclass.initComponent.call(this);
			},
			getValue : function() {
				if (this.getForm().isValid())
					return new Ext.data.Record(this.getForm().getValues());
				else
					throw Error("表单填写出错");
			}
		});
Ext.lams.AssetManager = Ext.extend(Ext.Panel, {
			grid : {},
			constructor : function(_cfg) {
				Ext.apply(this, _cfg || {});

				Ext.lams.AssetManager.superclass.constructor.call(this);
				this.grid.on("rowdb", this.onRowdbClick, this);
				this.grid.on("edit", this.onEditWin, this);
				this.grid.on("delete", this.onDeleteRow, this)
				this.qform.on("query",this.onQuery,this);
			},
			initComponent : function() {
				var me = this;
				var top = new Ext.lams.queryForm({
							region : 'north',
							height : 40,
							split : true,
							collapseMode : 'mini'
						});
				this.qform = top;
				var center = new Ext.lams.AssetsGrid({
							region : 'center'
						});
				this.grid = center;
				Ext.apply(this, {
							layout : 'border',
							frame : true,
							items : [this.qform, this.grid]
						});
				Ext.lams.AssetManager.superclass.initComponent.call(this);
			},
			createAddWin : function() {
				var me = this;
				if (!this.addWin) {
					var addWin = new Ext.lams.infoWin({
								title : '新建资产',
								width : '400',
								listeners : {
									submit : function(win, action) {
										// Ext.Msg.alert("INFO",
										// action.result["message"]);
										H.Msg.info(action.result["message"],
												function() {
													var r = this.getForm()
															.getValue();
													me.grid.getStore().add(r);
													this.getForm().clear();
													this.hide();
												});

									}
								}
							});
					this.addWin = addWin;
				}
			},
			createEditWin : function() {
				var me = this;
				if (!this.editWin) {
					var editWin = new Ext.lams.infoWin({
								title : '资产编辑',
								width : '400',
								listeners : {
									submit : function(win, action) {
										// Ext.Msg.alert("INFO",
										// action.result["message"]);
										H.Msg.info(action.result["message"],
												function() {
													try {
														var _r = this.getForm()
																.getValue();
														var _sr = me.grid
																.getSelectedItem();
														var _data = _sr.data;
														for (var _i in _data) {
															_sr.set(_i,
																	_r.get(_i));
														}
														_sr.commit();
														me.editWin.hide();
													} catch (_e) {
														// Ext.MessageBox.alert(
														// "ERROR",
														// _e.message);
														H.Msg.error(_e.message);
													}
												});

									}
								}
							});
					this.editWin = editWin;
				}
			},
			onQuery : function(btn) {
				try {
					btn.disable();
					var _r = this.qform.getValue();
					this.grid.loadData(_r, function() {
								btn.enable();
							});
				} catch (_e) {
					// Ext.Msg.alert("ERROR", _e.message);
					H.Msg.error(_e.message);
				}
			},
			onAddWin : function() {
				this.createAddWin();
				this.addWin.show();
			},
			onEditWin : function(_r) {
				this.createEditWin();
				try {
					// var sel = this.grid.getSelectedItem();
					this.editWin.getForm().setValue(_r);
					this.editWin.show();
				} catch (_e) {
					// Ext.Msg.alert("ERROR", _error.message);
					H.Msg.error(_e.message);
				}

			},
			onRowdbClick : function() {
				try {
					// if(!_r)_r = this.grid.getSelectRow()
					this.editWin.form.clear();
					this.editWin.show();
					this.editWin.form.setValue(this.grid.getSelectRow());

				} catch (_e) {
					// Ext.Msg.alert("Error", _err.message);
					H.Msg.error(_e.message);
				}
			}
		});